local skel = fk.CreateSkill{
  name = "tea__xingxue",
}
--local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__xingxue"] = "兴学",
  [":tea__xingxue"] = "当你于回合内首次使用一种牌名字数的牌时，你可以令至多X名角色各摸一张牌（X为你本回合发动此技能的次数），然后其中手牌数大于体力值的角色须将至少一张牌置于牌堆顶。",
  ["#tea__xingxue-invoke"] = "兴学：你可以选择至多 %arg 名角色，这些角色依次摸一张牌",
  ["#tea__xingxue-card"] = "兴学：你需要将至少一张牌置于牌堆顶"
}
---@type TrigSkelSpec<UseCardFunc>
local xingxue = {
  anim_type = "support",
  times = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryTurn) + 1
  end,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player == target and data.card and player.room.current == player then
      local len = Fk:translate(data.card.trueName, "zh_CN"):len()
      local turn_id = (player.room.logic:getCurrentEvent():findParent(GameEvent.Turn) or {}).id
      if turn_id then
        local events = player.room.logic:getEventsByRule(GameEvent.UseCard, 2, function (e)
          return len == Fk:translate(e.data.card.trueName or "", "zh_CN"):len()
        end, turn_id)
        return #events == 1
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local num = player:usedSkillTimes(skel.name, Player.HistoryTurn) + 1
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      targets = room.alive_players,
      min_num = 1,
      max_num = num,
      skill_name = skel.name,
      prompt = "#tea__xingxue-invoke:::" .. num
    })
    if #tos > 0 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos ---@type ServerPlayer[]
    room:sortByAction(tos)
    for _, p in ipairs(tos) do
      if not p.dead then
        p:drawCards(1, skel.name)
      end
    end
    tos = table.filter(tos, function (p)
      return #p:getCardIds("h") > p.hp and not p:isNude()
    end)
    for _, p in ipairs(tos) do
      if not p.dead and not p:isNude() then
        local cards = room:askToCards(p, {
          min_num = 1,
          max_num = 999,
          skill_name = skel.name,
          cancelable = false,
          include_equip = true,
          prompt = "#tea__xingxue-card"
        })
        if #cards > 0 then
          room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, skel.name, nil, false, p)
        end
      end
    end
  end
}
skel:addEffect(fk.CardUsing, xingxue)
-- Fk.generals["mouxusheng"]:addSkill(skel.name)
return skel